<?php
namespace Org;
use Think\Controller;
class Autoload extends Controller{
    //ThinkPHP的构造函数
    public function _initialize() {
        $this->auto_login(); //自动登录
        $this->member_info=$this->get_member_info();
        $this->member_id=$this->member_info['member_id'];
        //print_r($this->member_info);exit;
        $this->assign('member_info',$this->member_info);
        //dump($member_info);exit;
        //echo $member_info['member_id'];exit;
    }
    //自动登录 此处根据UC同步登录时传过来的uid进行处理，如果已经存在用户，则自动登录，否则，跳到完善资料页面，完善资料后写入本地数据库
    private function auto_login(){
        //cookie('uid',null);exit;
        //判断 cookie uid在而 member_id不在 则写入数据 并提示完善资料
        if(cookie('uid')!=''&&cookie('member_id')==''){
            $uid=cookie('uid');
            //查询是否存在
            $Member=M('Member');
            $count=$Member->where('member_id='.$uid)->count();
            if($count==0){
                //连接UC端
                $UC=M('Members','uc_','DB_CONFIG');
                $usertype=$UC->where('uid='.$uid)->getField('usertype');
                $username=$UC->where('uid='.$uid)->getField('username');
                $password=$UC->where('uid='.$uid)->getField('password');
                $email=$UC->where('uid='.$uid)->getField('email');
                $mobile=$UC->where('uid='.$uid)->getField('mobile');
                //写入数据库
                $data['member_id']=$uid;
                $data['uc_username']=$username;
                $data['uc_usertype']=$usertype;
                $data['member_passwd']=$password;
                $data['member_email']=$email;
                $Member->add($data);
                //设置session cookie
                session('member_id',$uid);
                session('member_email',$email);
                cookie('member_id',$uid,3600);
                cookie('member_email',$email,3600);
            }
        }
    }
    //获取 用户信息
    private function get_member_info(){
        //
        //设置 member_info
        if(cookie('member_id')){
            $member_info['member_id']=cookie('member_id');
        }
        if(cookie('member_email')){
            $member_info['member_email']=cookie('member_email');
        }
        if(cookie('member_mobile')){
            $member_info['member_mobile']=cookie('member_mobile');
        }
        if(session('member_id')){
            $member_info['member_id']=session('member_id');
        }
        if(session('member_email')){
            $member_info['member_email']=session('member_email');
        }
        if(session('member_mobile')){
            $member_info['member_mobile']=session('member_mobile');
        }
        //dump(cookie());exit;
        //dump($member_info);exit;
        return $member_info;
    }

}